import { handleActions } from 'redux-actions'
import Taro from '@tarojs/taro'
import { LOGIN } from '../types'
import Enum from '../../constants/Enum'

const getinitialLoginState = () => ({
  session: Taro.getStorageSync(Enum.session_key),
})

const initialState = getinitialLoginState()

const reducer = {
  [LOGIN]: (state, action) => {
    Taro.setStorageSync(
      Enum.session_key,
      JSON.stringify(action.payload.data)
    )
    let data = {}
    if (action.payload && action.payload.data) {
      data = action.payload.data
    }
    return {
      ...state,
      session: data
    }
  }
}

export default handleActions(
  reducer,
  initialState
)
